#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -z "$1" ]; then
    echo "Usage1: patch_from_baidu PATCHFILE (generated by 'git diff --no-prefix')"
    echo "Usage2: patch_from_baidu GIT_DIR COMMIT"
    exit 1
fi
PATCHFILE=$1
if [ -d "$1/.git" ]; then
    if [ -z "$2" ]; then
        echo "Second argument must be a git commit"
        exit 1
    fi
    CURRENT_DIR=`pwd`
    GIT_DIR=$1
    COMMIT=$2
    PATCHFILE=$CURRENT_DIR/$COMMIT.patch
    echo "*** Generating diff of $GIT_DIR@$COMMIT"
    cd $GIT_DIR && git diff --no-prefix $COMMIT^! > $PATCHFILE
    cd $CURRENT_DIR
fi

MODIFIED_PATCHFILE=$(basename $(basename $PATCHFILE .patch) .diff).from_baidu.patch

# guess prefix of test files
TEST_PREFIX="test_"
TEST_SUFFIX=
if fgrep -q " src/baidu/rpc/" $PATCHFILE; then
    TEST_PREFIX="brpc_"
    TEST_SUFFIX="_unittest"
elif fgrep -q " bthread/" $PATCHFILE; then
    TEST_PREFIX="bthread_"
    TEST_SUFFIX="_unittest"
elif fgrep -q " bvar/" $PATCHFILE; then
    TEST_PREFIX="bvar_"
    TEST_SUFFIX="_unittest"
fi

cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \
            -e 's/\<baidu\/rpc\//brpc\//g' \
            -e 's/\<baidu\-rpc\([^-]\)/brpc\1/g' \
            -e 's/\<test\/test_bthread\.cpp/test\/bthread_unittest.cpp/g' \
            -e 's/\<test\/test_object_pool\.cpp/test\/object_pool_unittest.cpp/g' \
            -e 's/\<test\/test_resource_pool\.cpp/test\/resource_pool_unittest.cpp/g' \
            -e "s/\<test\/test_\(\S*\)\.cpp/test\/${TEST_PREFIX}\1${TEST_SUFFIX}.cpp/g" \
            -e 's/\<namespace \+baidu *{/namespace brpc {/g' \
            -e 's/\<namespace \+rpc *{//g' \
            -e 's/} *\/\/ \+namespace \+baidu/} \/\/ namespace brpc/g' \
            -e 's/} *\/\/ \+namespace \+rpc\>//g' \
            -e 's/\<baidu::rpc::/brpc::/g' \
            -e 's/\<rpc::/brpc::/g' \
            -e 's/\<base::/butil::/g' \
            -e 's/\<BAIDU_RPC_/BRPC_/g' \
            -e 's/\<protocol\/\(\S*\)\.proto/src\/\1.proto/g' \
            -e 's/ bthread_cond\.cpp/ src\/bthread\/condition_variable.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.cpp/ src\/bthread\/\1.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.h/ src\/bthread\/\1.h/g' \
            -e 's/ bthread\.\(h\|cpp\)/ src\/bthread\/bthread.\1/g' \
            -e 's/<\(brpc\/[^>]*\)>/"\1"/g' \
            -e 's/<\(bvar\/[^>]*\)>/"\1"/g' \
            -e 's/<base\/\([^>]*\)>/"butil\/\1"/g' \
            -e 's/"base\/\([^"]*\)"/"butil\/\1"/g' \
            -e 's/<\(bthread\/[^>]*\)>/"\1"/g' \
            -e 's/<\(mcpack2pb\/[^>]*\)>/"\1"/g' \
            -e 's/\<protobuf_json\>/json2pb/g' \
            -e 's/src\/json_to_pb/src\/json2pb\/json_to_pb/g' \
            -e 's/src\/pb_to_json/src\/json2pb\/pb_to_json/g' \
            -e 's/test\/test_protobuf_json/test\/brpc_protobuf_json_unittest/g' \
            -e 's/ base\// src\/butil\//g' \
            -e 's/ bthread\// src\/bthread\//g' \
            -e 's/ bvar\// src\/bvar\//g' \
            > $MODIFIED_PATCHFILE
EXTRA_ARGS=
if [ -z "$DO_RUN" ]; then
    EXTRA_ARGS="--dry-run $EXTRA_ARGS"
    echo "*** This is a dry-run. To really apply, run: DO_RUN=1 tools/patch_from_baidu $1"
fi
patch -p0 -u -l $EXTRA_ARGS < $MODIFIED_PATCHFILE
if [ ! -z "$DO_RUN" ]; then
    REJ_FILES=`find . -name "*.rej"`
    if [ ! -z "$REJ_FILES" ]; then
        echo "==== The patching is not done yet! Apply following rej files manually ===="
        echo $REJ_FILES
    fi
fi
